package com.smdk.dsminio.utils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author 神秘的凯
 * Created by fujiakai on 2017/3/14.
 * ajax请求返回结果集
 */
@NoArgsConstructor
@ApiModel(value = "数据返回模型")
public class AjaxResult<T> implements Serializable {
    public  static final long serialVersionUID=1L;
    //成功返回码
    public static final int SUCCESS = 200;
    //失败返回码
    public static final int FAILURE = 500;

    //为空提示
    public static final int ISNULL = 800;

    //更新失败提示
    public static final String UPDAREOKMSG = "更新失败";
    //更新成功提示
    public static final String UPDAREFAILMSG = "更新成功";
    //删除成功提示
    public static final String DELETEOKMMSG = "删除成功";
    //删除失败提示
    public static final String DELETEFAILMSG = "删除失败";
    //添加成功提示
    public static final String ADDOKMMSG = "添加成功";
    //添加失败提示
    public static final String ADDFAILMSG = "添加失败";
    //查询成功提示
    public static final String SEARCHOKMMSG = "数据加载成功";
    //查询失败提示
    public static final String SEARCHFAILMSG = "暂无数据";

    //结果不加密判断码
    public static final int RESULTVIEW = 900;
    //数据加密KEY
    public static final String DEAPASSWORD = "HXTK20190102_1.0";
    /**
     * 返回类型
     */
    @ApiModelProperty(value = "返回类型")
    @Getter
    @Setter
    private int returnCode;
    /**
     * 消息
     */
    @ApiModelProperty(value = "消息")
    @Getter
    @Setter
    private String msg;

    /**
     * 数据
     */
    @ApiModelProperty(value = "数据")
    @Getter
    @Setter
    private T data;


    public AjaxResult(String msg, int returnCode) {
        this.setMsg(msg);
        this.returnCode = returnCode;
    }


    public AjaxResult(T data, String msg, int returnCode) {
        this.data = data;
        this.setMsg(msg);
        this.returnCode = returnCode;

    }


    public static AjaxResult success(Object data, String msg) {
        return new AjaxResult(data, msg, SUCCESS);
    }

    public static AjaxResult success(Object data) {
        return new AjaxResult(data, SEARCHOKMMSG, SUCCESS);
    }


    public static AjaxResult fail(Object data, String msg, int returnCode) {
        return new AjaxResult(data, msg, returnCode);
    }


    public static AjaxResult fail(Object data, String msg) {
        return new AjaxResult(data, msg, FAILURE);
    }


    public static AjaxResult fail(String msg, int returnCode) {
        return new AjaxResult(msg, returnCode);
    }

    public static AjaxResult fail(String msg) {
        return new AjaxResult(msg, FAILURE);
    }

    public static <T> AjaxResult queryResult(T data, String msg) {
        if (data instanceof List) {
            if (!MapListIsNotNull.ListIsNotNull((List) data)) {
                return new AjaxResult(data, msg, ISNULL);
            } else {
                return new AjaxResult(data, msg, SUCCESS);
            }

        } else if (data instanceof Map) {
            if (!MapListIsNotNull.ListIsNotNull((List) data)) {
                return new AjaxResult(data, msg, ISNULL);
            } else {
                return new AjaxResult(data, msg, SUCCESS);
            }
        } else if (data instanceof String && (data == null || data.equals(""))) {
            return new AjaxResult(data, msg, SUCCESS);
        } else {
            return new AjaxResult(data, msg, ISNULL);
        }

    }


    public static <T> AjaxResult updateResult(T data) {

        if (data == null) {
            return new AjaxResult(data, UPDAREOKMSG, FAILURE);
        }
        if (data instanceof Boolean) {
            if ((Boolean) data) {
                return new AjaxResult(data, UPDAREFAILMSG, SUCCESS);
            } else {
                return new AjaxResult(data, UPDAREOKMSG, FAILURE);
            }
        }
        if (!data.toString().equals("0")) {
            return new AjaxResult(data, UPDAREFAILMSG, SUCCESS);
        }
        return new AjaxResult(data, UPDAREOKMSG, FAILURE);
    }

    public static <T> AjaxResult<T> addResult(T data) {

        if (data == null) {
            return new AjaxResult(data, ADDFAILMSG, FAILURE);
        }
        if (!data.toString().equals("0")) {
            return new AjaxResult(data, ADDOKMMSG, SUCCESS);
        }
        return new AjaxResult(data, ADDFAILMSG, FAILURE);
    }

    public static <T> AjaxResult deleteResult(T data) {
        if (data == null) {
            return new AjaxResult(data, DELETEFAILMSG, FAILURE);
        }

        if (!data.toString().equals("0")) {
            return new AjaxResult(data, DELETEOKMMSG, SUCCESS);
        }
        if (data instanceof Boolean) {
            if ((Boolean) data) {
                return new AjaxResult(data, DELETEOKMMSG, SUCCESS);
            } else {
                return new AjaxResult(data, DELETEFAILMSG, FAILURE);
            }
        }
        return new AjaxResult(data, DELETEFAILMSG, FAILURE);
    }


}
